layui.use(['form', 'layer'], function(){
    var form = layui.form
        ,layer = layui.layer;

    var treeObj = null;
    $(document).ready(function(){
        var roleId = document.getElementById("roleId").value;
        $.ajax({
            async: false,
            cache: false,
            type: "POST",
            url: "admin/showRoleMenu?roleId=" + roleId,
            error: function (data) {
                alert("请求异常！");
            },
            success: function (data) {
                if (data.errNo != 0) {
                    alert(data.errMsg);
                    return;
                }
                var treeNodes = data.obj;
                treeObj = $.fn.zTree.init($("#treeDemo"), setting, treeNodes);
            }
        });

        setCheck();
        $("#py").bind("change", setCheck);
        $("#sy").bind("change", setCheck);
        $("#pn").bind("change", setCheck);
        $("#sn").bind("change", setCheck);
    });

    form.on('submit(editRole)', function(data){
        var menuIdArr = "";
        var nodes = treeObj.getCheckedNodes();
        for (var i = 0; i < nodes.length; i++) {
            menuIdArr += nodes[i].id + ",";
        }
        $.ajax({
            url: "admin/addOrUpdateRole",
            data: {
                id: data.field.roleId,
                name:data.field.name,
                code:data.field.code,
                remake:data.field.remake,
                menuIdArr: menuIdArr
            },
            type: "POST",
            success: function(response){
                if(response.errNo == 0){
                    layer.alert(response.errMsg, {
                        icon: 1, yes: function () {
                            parent.layui.table.reload('roleList');
                            parent.layer.closeAll('iframe'); //再执行关闭
                        }
                    })
                }else {
                    layer.alert(response.errMsg, {icon: 2});
                }
            },
            error: function () {
                parent.layer.alert("请求异常！", {icon: 2});
            }
        });
        return false;
    });

});


var setting = {
    check: {
        enable: true
    },
    view: {
        showLine: true,
        selectedMulti: false
    },
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "pId"
        }
    }
};

var zNodes =[
    { id:1, pId:0, name:"随意勾选 1", open:true},
    { id:11, pId:1, name:"随意勾选 1-1", open:true},
    { id:111, pId:11, name:"随意勾选 1-1-1"},
    { id:112, pId:11, name:"随意勾选 1-1-2"},
    { id:12, pId:1, name:"随意勾选 1-2", open:true},
    { id:121, pId:12, name:"随意勾选 1-2-1"},
    { id:122, pId:12, name:"随意勾选 1-2-2"},
    { id:2, pId:0, name:"随意勾选 2", checked:true, open:true},
    { id:21, pId:2, name:"随意勾选 2-1"},
    { id:22, pId:2, name:"随意勾选 2-2", open:true},
    { id:221, pId:22, name:"随意勾选 2-2-1", checked:true},
    { id:222, pId:22, name:"随意勾选 2-2-2"},
    { id:23, pId:2, name:"随意勾选 2-3"}
];

var code;

function setCheck() {
    var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
    type = { "Y" : "ps", "N" : "ps" };
    zTree.setting.check.chkboxType = type;
    showCode('setting.check.chkboxType = { "Y" : "' + type.Y + '", "N" : "' + type.N + '" };');
}
function showCode(str) {
    if (!code) code = $("#code");
    code.empty();
    code.append("<li>"+str+"</li>");
}

